layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var form = layui.form;


    var selectFarmId;

    /**
     * 物料管理
     */
    var Materiel = {
        tableId: "materielTable"
    };

    /**
     * 初始化表格的列
     */
    Materiel.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'materielId', hide: true, title: '物料ID'},
            {field: 'materielCode', sort: true, title: '物料编码'},
            {field: 'title', sort: true, title: '名字'},
            {field: 'number', sort: true, title: '数量', templet:function (d) {
                    return '<span class="layui-badge layui-bg-blue">  '+d.number+'   </span>';
                }},

            {field: 'type', sort: true, title: '类型', templet:function (d) {
                    if (d.type === 0) {
                        return  '<span class="layui-badge">普通原料</span>';
                    } else if (d.type === 1) {
                        return  '<span class="layui-badge layui-bg-cyan">饲料</span>';
                    }  else if (d.type === 2) {
                        return  '<span class="layui-badge layui-bg-green">药物</span>';
                    }  else {
                        return  '<span class="layui-badge">普通原料</span>';
                    }
                }},
            {field: 'price', sort: true, title: '单价'},
            {field: 'unit', sort: true, title: '单位'},
            {field: 'specs', sort: true, title: '规格'},
            {field: 'buyCode', sort: true, title: '购买编码'},
            {field: 'batchNumber', sort: true, title: '批号'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Materiel.search = function () {
        var queryData = {};
        queryData['title'] = $("#title").val();
        queryData['type'] = $("#type").val();
        queryData['source'] = $("#source").val();
        queryData['materielCode'] = $("#materielCode").val();
        queryData['buyCode'] = $("#buyCode").val();

        table.reload(Materiel.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    /**
     * 弹出添加对话框
     */
    Materiel.openAddDlg = function () {

        window.location.href = Feng.ctxPath + '/api/materiel/edit';

    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    Materiel.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/api/materiel/edit?materielId=' + data.materielId;
    };

    /**
     * 导出excel按钮
     */
    Materiel.exportExcel = function () {
        var checkRows = table.checkStatus(Materiel.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    Materiel.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/api/materiel/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Materiel.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("materielId", data.materielId);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Materiel.tableId,
        url: Feng.ctxPath + '/api/materiel/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        method: "post",
        cols: Materiel.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Materiel.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Materiel.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        Materiel.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + Materiel.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Materiel.openEditDlg(data);
        } else if (layEvent === 'delete') {
            Materiel.onDeleteItem(data);
        }
    });
});
